/**
 * Author : duchuynh
 * Nov 7, 2011
 */
package wte.rnd.buildingontology.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.Border;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;

import com.hp.hpl.jena.ontology.Ontology;

import wte.rnd.buildingontology.core.OntologyModel;
import wte.rnd.buildingontology.resource.ImageResource;

import edu.stanford.smi.protegex.owl.jena.JenaOWLModel;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;
import edu.stanford.smi.protegex.owl.model.impl.DefaultRDFSNamedClass;

public class ClassTreePanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTree clssTree;
	private JenaOWLModel owlModel;	
	private JScrollPane tree;
	static ArrayList<OWLNamedClass> classesArrayList;
	
	public ClassTreePanel(JenaOWLModel owl){
		super(new BorderLayout());
		this.owlModel = owl;
		TreeNode node = this.convertOWLtoTree();
		clssTree = new JTree(node);
		clssTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		clssTree.addTreeSelectionListener(new TreeSelectionListener() {
			@Override
			public void valueChanged(TreeSelectionEvent arg0) {
				// TODO Auto-generated method stub
				TreeNode node = (TreeNode)clssTree.getLastSelectedPathComponent();
				if (node != null){
					MainFrame.listChosenClassEntities = OntologyModel.getListIndividualOfClass(node.getIdClss(), owlModel);
					MainFrame.repaintFilesListEntitites();
					OWLNamedClass owlNamedClass = OntologyModel.queryOWLClassByNamed(node.getIdClss(), owlModel);
					int count = owlNamedClass.getInstanceCount(false);
					MainFrame.entityIDText.setText(MainFrame.entityNameText.getText().toLowerCase().replace(' ', '_'));
					MainFrame.entityClassText.setText(node.getNameClss());
				}
				else return;
			}
		});
		ImageIcon tutorialIcon = new ImageIcon(ImageResource.IconClass);
		if (tutorialIcon != null) {
			clssTree.setCellRenderer(new MyRenderer(tutorialIcon));
		}
		tree = new JScrollPane(clssTree);
		this.add(tree, BorderLayout.CENTER);
	}	
	public TreeNode getSelectTreeNode(){
		TreeNode node = (TreeNode)clssTree.getLastSelectedPathComponent();
		return node;
	}
	public TreeNode convertOWLtoTree(){
		OWLNamedClass thingClass = owlModel.getOWLThingClass();
		TreeNode root = new TreeNode("Thing", "Root");
		Collection subClsses = thingClass.getSubclasses(false);
		try {
			for (Iterator it = subClsses.iterator(); it.hasNext();) {
				DefaultRDFSNamedClass cls = (DefaultRDFSNamedClass) it.next();
				if (!cls.isSystem()){
					TreeNode childNode = buildTreeFromThingClass(cls);
					root.add(childNode);
				}
			}
		} catch (Exception e) {
			System.out.println("");
		}			
		return root;
	}		
	
	public TreeNode buildTreeFromThingClass(DefaultRDFSNamedClass parentOWL){
		//create TreeNode for parent node
		TreeNode parentNode = new TreeNode(parentOWL.getLocalName(), parentOWL.getLabels().toArray()[0].toString());
		if(parentOWL.getSubclassCount() != 0){
			Collection subClsses = parentOWL.getSubclasses(false);
			try {
				for (Iterator it = subClsses.iterator(); it.hasNext();) {
					DefaultRDFSNamedClass cls = (DefaultRDFSNamedClass) it.next();
					TreeNode childNode = buildTreeFromThingClass(cls);
					parentNode.add(childNode);					
				}
			} catch (Exception e) {
				System.out.println("Error addClassesIntoArrayListByClass");
			}			
		}	
		return parentNode;
	}
	
	public static void addClassesIntoArrayListByClass(OWLNamedClass parentClass) {
		Collection classes = parentClass.getSubclasses(false);
		try {
			for (Iterator it = classes.iterator(); it.hasNext();) {
				DefaultRDFSNamedClass cls = (DefaultRDFSNamedClass) it.next();
				if (!cls.isSystem()) {
					classesArrayList.add((OWLNamedClass) cls);
					if (cls.getSubclassCount() != 0) {
						addClassesIntoArrayListByClass((OWLNamedClass) cls);
					}
				}
			}
		} catch (Exception e) {
			System.out.println("Error addClassesIntoArrayListByClass");
		}
	}
	public JScrollPane getTreePanel(){
		return this.tree;
	}
	public JTree getClssTree() {
		return clssTree;
	}
	public void setClssTree(JTree clssTree) {
		this.clssTree = clssTree;
	}
	public JenaOWLModel getOwlModel() {
		return owlModel;
	}
	public void setOwlModel(JenaOWLModel owlModel) {
		this.owlModel = owlModel;
	}
	public JScrollPane getTreeView() {
		return tree;
	}
	public void setTreeView(JScrollPane treeView) {
		this.tree = treeView;
	}
	public static ArrayList<OWLNamedClass> getClassesArrayList() {
		return classesArrayList;
	}
	public static void setClassesArrayList(ArrayList<OWLNamedClass> classesArrayList) {
		ClassTreePanel.classesArrayList = classesArrayList;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	private class MyRenderer extends DefaultTreeCellRenderer {
        Icon tutorialIcon;
 
        public MyRenderer(Icon icon) {
            tutorialIcon = icon;
            this.setIcon(tutorialIcon);
        }
 
        public Component getTreeCellRendererComponent(
                            JTree tree,
                            Object value,
                            boolean sel,
                            boolean expanded,
                            boolean leaf,
                            int row,
                            boolean hasFocus) {
 
            super.getTreeCellRendererComponent(
                            tree, value, sel,
                            expanded, leaf, row,
                            hasFocus);
             
            return this;
        }
	}	
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JenaOWLModel owl = OntologyModel.loadOntologyModel("/media/DATA/Ontology Engineering/P2Ont/Ontology OWL/OntoSave.owl");
		ClassTreePanel f = new ClassTreePanel(owl);
		frame.getContentPane().add(f);
		frame.setSize(800,800);
		frame.setEnabled(true);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

